package com.zidian.teacher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.zidian.teacher.R;
import com.zidian.teacher.adapter.CourseSelectAdapter;
import com.zidian.teacher.bean.CourseListBean;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ProgressBarBlackUtil;
import com.zidian.teacher.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

import com.zidian.teacher.common.ToolBarActivity;

/**
 * Created by Administrator on 2016/9/5 0005.
 */
public class CourseSelectActivity extends ToolBarActivity {

    @Bind(R.id.list)
    RecyclerView mRecyclerView;
    private CourseSelectAdapter adapter;
    private List<CourseListBean.DataBean> data = new ArrayList<>();
    private String courseId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_class_select);
        setTitle("选择课程");
        ButterKnife.bind(this);
        courseId = getIntent().getStringExtra("courseId");
        adapter = new CourseSelectAdapter(this, data);
        initView();
        initData();
    }

    public void initView() {
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d("test", "StateChanged = " + newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.d("test", "onScrolled");

                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    Log.d("test", "loading executed");

                }
            }
        });


        //添加点击事件
        adapter.setOnItemClickListener(new CourseSelectAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, ImageView imageView) {

                Intent intent = new Intent();
                intent.putExtra("className", data.get(position).getCourseName());
                intent.putExtra("courseId", data.get(position).getCourseId());
                setResult(1001, intent);
                finish();
//                startActivity(InteractionAskDetailAct.class);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

    }


    public void initData() {
        getCourse();
    }

    /**
     * 获取测试数据
     */
    private void getData() {
        int index = 0;
        for (int i = 0; i < data.size(); i++) {
            Map<String, String> map = new HashMap<>();
            if (courseId.length() > 0) {
                if (data.get(i).getCourseId().equals(courseId)) {
                    index = i;

                } else {
                }

            } else {

            }
        }
        adapter.setMposition(index);
        adapter.notifyDataSetChanged();
    }

    /**
     * 获取测试数据
     */
    private void getCourse() {
        String loginUrl = Constant.URL_GOAL + "work/course";
        HashMap<String, String> map = new HashMap<>();
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("token", GetUserInfo.getToken(context));

        ProgressBarBlackUtil.loadShow(context);

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(context, "获取失败！");
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                ProgressBarBlackUtil.loadCancel();
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {

                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        return;
                    } else {
                    }

                } catch (JSONException e) {

                }

                Gson gson = new Gson();
                CourseListBean model = gson.fromJson(response, CourseListBean.class);
                if (model.isSuccess()) {
                    data.addAll(model.getData());
                    getData();
                }

            }
        });
    }


}
